<template>
    <div class="product-list" v-loading="loading">
        <div class="product-item" :style="{ marginBottom: props.gap + 'px' }" v-for="item in renderList" :key="item.id">
            <div class="product-img" @click="onPreview(item, item.templatestype_id || props.type)">
                <NuxtImg fit="cover" class="cover-img" :src="item.file +
                    '?x-oss-process=image/watermark,image_' +
                    toBase64(
                        'water/logo_white.png?x-oss-process=image/resize,P_30'
                    ) +
                    ',g_center,t_60/format,webp'
                    " loading="lazy" alt="北京华锐云空间" />
                <img class="preview" :src="`${_oss_}images/works/preview.png`" alt="北京华锐云空间" />

                <div class="review-num">
                    <img :src="`${_oss_}images/works/icon-view-white.png`" alt="北京华锐云空间" />
                    <span>{{ item.page_view || 0 }}</span>
                </div>
            </div>

            <div class="product-info">
                <div class="product-top">
                    <div class="product-name" :title="item.name">
                        {{ item.name }}
                    </div>

                    <img class="share-btn" :src="`${_oss_}images/icon-share-btn.png`"
                        @click="onShare(item, item.templatestype_id || props.type)" />
                </div>
                <div class="product-middle">
                    <div class="icon-group">
                        <div class="icon-item like-item" :class="{ active: item.is_like }" @click="onLike(item)">
                            <img v-if="item.is_like" :src="`${_oss_}images/works/icon-like-active.png`" alt="北京华锐云空间" />
                            <img v-else :src="`${_oss_}images/works/icon-like.png`" alt="北京华锐云空间" />
                            <span>{{ item.like || 0 }}</span>
                        </div>

                        <div class="icon-item star-item" :class="{ active: item.is_star }" @click="onStar(item)">
                            <img v-if="item.is_star" :src="`${_oss_}images/works/icon-star-active.png`" alt="北京华锐云空间" />
                            <img v-else :src="`${_oss_}images/works/icon-star.png`" alt="北京华锐云空间" />
                            <span>{{ item.star || 0 }}</span>
                        </div>

                        <div class="icon-item comment-item" @click="onComment(item)">
                            <img :src="`${_oss_}images/works/icon-comment.png`" alt="北京华锐云空间" />
                            <span>{{ item.comment || 0 }}</span>
                        </div>

                        <div class="icon-item share-item">
                            <img :src="`${_oss_}images/works/icon-share.png`" alt="北京华锐云空间" />
                            <span>{{ item.share || 0 }}</span>
                        </div>
                    </div>
                </div>

                <div class="product-bottom">
                    <div class="publisher" @click="GoToPageHome(item.consumer_id)"
                        :title="item.user.nickname || item.user.mobile">
                        <img class="avatar" :src="item.user.avatar ||
                            `${_oss_}images/user/default-avatar.png`
                            " />
                        {{ item.user.nickname || item.user.mobile }}
                    </div>
                    <div class="btn-default" @click="
                        onMakeSame(
                            item,
                            item.templatestype_id || props.type
                        )
                        ">
                        制作同款
                    </div>
                </div>
            </div>
        </div>
    </div>

    <div class="see-more" v-if="props.max && props.list.length > props.max" @click="isExpand = !isExpand">
        {{ isExpand ? "收起" : "查看更多" }}
        <el-icon>
            <ArrowUp v-if="isExpand" />
            <ArrowDown v-else />
        </el-icon>
    </div>
</template>
<script setup>
import { useContent } from ".";
import { useLike, useStar } from "~/hooks";
import { ArrowDown, ArrowUp } from "@element-plus/icons-vue";
const { onShare, onPreview, onComment, onMakeSame } = useContent();
const { onLike } = useLike();
const { onStar } = useStar();

const props = defineProps({
    list: {
        type: Array,
        default: () => [],
    },
    gap: {
        type: Number,
        default: 12,
    },
    max: {
        type: [String, Number],
        default: 0,
    },
    type: {
        type: Number,
        default: 1,
    },
    loading: {
        type: Boolean,
        default: false,
    },
});

const isExpand = ref(false);

const renderList = computed(() => {
    if (props.max) {
        return isExpand.value ? props.list : props.list.slice(0, props.max);
    }

    return props.list;
});
</script>
<style lang="scss" scoped>
.product-list {
    margin-top: 20px;
    display: flex;
    flex-wrap: wrap;

    .product-item {
        position: relative;
        width: 288px;
        background-color: #fff;
        border-radius: 10px;
        overflow: hidden;
        margin-right: 16px;
        // margin-bottom: 12px;
        user-select: none;
        box-shadow: 0px 0px 10px 0px rgba(1, 23, 86, 0.1);
        transition: all 0.5s;

        &:hover {
            transform: translateY(-10px);
            box-shadow: 0px 0px 20px 0px rgba(1, 23, 86, 0.5);
        }

        &:nth-of-type(4n) {
            margin-right: 0;
        }

        .product-img {
            position: relative;
            width: 100%;
            height: 156px;
            overflow: hidden;
            cursor: pointer;

            .product-name {
                width: 100%;
                position: absolute;
                left: 50%;
                top: 50%;
                font-size: 26px;
                color: #fff;
                -webkit-text-stroke: 1px #000;
                font-weight: bold;
                transform: translate(-50%, -50%);
                z-index: 1;
                text-align: center;
            }

            &:hover {
                .cover-img {
                    transform: scale(1.4);
                }

                .preview {
                    opacity: 1;
                }
            }

            .review-num {
                position: absolute;
                right: 7px;
                top: 8px;
                display: flex;
                align-items: center;
                color: #fff;
                font-size: 14px;
                background-color: rgba(0, 0, 0, 0.3);
                padding: 6px 7px;
                border-radius: 4px;

                img {
                    margin-right: 5px;
                }
            }

            .cover-img {
                width: 100%;
                height: 100%;
                object-fit: cover;
                transition: all 0.4s linear;
            }

            .preview {
                opacity: 0;
                position: absolute;
                left: 0;
                top: 0;
                transition: all 0.4s linear;
                z-index: 10;
            }
        }

        .product-info {
            padding: 0 12px 15px;

            .product-top {
                display: flex;
                align-items: center;
                padding: 12px 0;
                border-bottom: 1px solid #f1f1f1;

                .product-name {
                    font-size: 18px;
                    font-weight: bold;
                    width: 100%;
                    overflow: hidden;
                    text-overflow: ellipsis;
                    white-space: nowrap;
                    padding-left: 5px;
                }

                .share-btn {
                    cursor: pointer;
                }
            }

            .product-middle {
                display: flex;
                align-items: center;
                margin-top: 12px;
                padding-left: 4px;

                .publisher {
                    color: #5085fb;
                    margin-left: auto;
                    font-size: 16px;
                    white-space: nowrap;
                    text-overflow: ellipsis;
                    overflow: hidden;
                }

                .icon-group {
                    display: flex;
                    align-items: center;
                    color: #333;
                    font-size: 14px;
                    font-weight: 500;

                    img {
                        margin-right: 4px;
                    }

                    .icon-item {
                        display: inline-flex;
                        align-items: center;
                        margin-right: 20px;

                        &.like-item,
                        &.star-item {
                            cursor: pointer;

                            img {
                                transition: all 0.2s;
                            }

                            &:active img {
                                transform: scale(1.3);
                            }
                        }

                        &.view-item {
                            img {
                                width: 17px;
                            }
                        }

                        &.like-item {
                            img {
                                width: 14px;
                            }
                        }

                        &.star-item {
                            img {
                                width: 14px;
                            }
                        }

                        &.comment-item {
                            cursor: pointer;

                            img {
                                width: 13px;
                            }
                        }

                        &.share-item {
                            img {
                                width: 12px;
                                height: 12px;
                            }
                        }
                    }
                }

                .make-same-btn {
                    margin-left: auto;
                }
            }

            .product-bottom {
                display: flex;
                align-items: center;
                justify-content: space-between;
                margin-top: 8px;

                .publisher {
                    display: flex;
                    align-items: center;
                    cursor: pointer;

                    .avatar {
                        width: 20px;
                        height: 20px;
                        margin-right: 5px;
                        border-radius: 50%;
                        object-fit: cover;
                    }

                    font-size: 14px;
                    color: #5085fb;
                }

                .btn-default {
                    display: flex;
                    align-items: center;
                    justify-content: center;
                    width: 74px;
                    height: 30px;
                    background: #e7e7e7;
                    border-radius: 4px;
                    font-size: 14px;
                    color: #333;
                    cursor: pointer;
                }
            }
        }

        .product-title {
            font-size: 18px;
            color: #333;
        }
    }
}

.see-more {
    display: flex;
    align-items: center;
    justify-content: flex-end;
    cursor: pointer;
    color: #5085fb;
}
</style>
